
import java.io.IOException;
import java.util.Calendar;
import lejos.nxt.LightSensor;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;
import lejos.nxt.UltrasonicSensor;
import lejos.nxt.remote.NXTCommand;
import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTConnector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * panelConector.java
 *
 * Created on 22/07/2011, 17:44:06
 */



/**
 *
 * @author Victor
 */
public class PanelConector extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = -4318528383177385386L;
	/** Creates new form panelConector */
    public PanelConector() {
        initComponents();
        botonConectar.setEnabled(true);
        botonDesconectar.setEnabled(false);
    }

    static public boolean estaConectado(){
        return PanelConector.conectado;
    }
    
    static public UltrasonicSensor getSensorUltrasonico(){
        return PanelConector.sensorUltrasonico;
    };
    
    static public LightSensor getSensorLuz(){
        return PanelConector.sensorLuz;
    };
    
    static public TouchSensor getSensorTacto(){
        return PanelConector.sensorTacto;
    };



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonDesconectar = new javax.swing.JButton();
        botonConectar = new javax.swing.JButton();
        jlEstado = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setPreferredSize(new java.awt.Dimension(681, 47));

        botonDesconectar.setText("Desconectar");
        botonDesconectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDesconectarActionPerformed(evt);
            }
        });

        botonConectar.setText("Conectar");
        botonConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonConectarActionPerformed(evt);
            }
        });

        jlEstado.setForeground(new java.awt.Color(51, 204, 255));
        jlEstado.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlEstado.setText("Desconectado");
        jlEstado.setToolTipText("Desconectado");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonConectar)
                .addGap(18, 18, 18)
                .addComponent(botonDesconectar)
                .addGap(18, 18, 18)
                .addComponent(jlEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonConectar)
                    .addComponent(botonDesconectar)
                    .addComponent(jlEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(11, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

	public static void pausarKeepAlive(boolean b) {
    	if (conectado){
    		if (b){
	    		t2.interrupt();
	    	} else {
	    		t2.start();
	    	}
    	}
	}
    
    public void keepAlive(){
        Motor.A.getTachoCount();
        System.out.println("ping...");
    }
    
	private void botonDesconectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDesconectarActionPerformed
        PanelConector.conectado = false;
            botonDesconectar.setEnabled(PanelConector.conectado);
            botonConectar.setEnabled(!PanelConector.conectado);
            try {
                    conn.close();
            } catch (IOException e1) {
                    e1.printStackTrace();
                    PanelConector.conectado = true;
                    botonConectar.setEnabled(!PanelConector.conectado);
                    botonDesconectar.setEnabled(PanelConector.conectado);
            }
            
            Calendar calendario = Calendar.getInstance();
            int hora =calendario.get(Calendar.HOUR_OF_DAY);
            int minutos = calendario.get(Calendar.MINUTE);
            int segundos = calendario.get(Calendar.SECOND);
            
            jlEstado.setText("Desconectado: "+hora + ":" + minutos + ":" + segundos);
            jlEstado.setToolTipText("Desconectado");
            
//            t2.interrupt();
            pausarKeepAlive(true);
//            jTA_Log.append("----------------\n");
//            jTA_Log.append("Desconectado al NXT "+ Calendar.HOUR_OF_DAY + ":" +
//                            Calendar.MINUTE + ":" + Calendar.SECOND + "\n");
//        jTA_Log.append("================\n\n");
    }//GEN-LAST:event_botonDesconectarActionPerformed

    private void botonConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonConectarActionPerformed
        botonConectar.setEnabled(!PanelConector.conectado);
        botonDesconectar.setEnabled(PanelConector.conectado);
        jlEstado.setText("Conectando...");
        jlEstado.setToolTipText("Conectando...");
        try {
        	conn = new NXTConnector();
        	if (!conn.connectTo("NXT", NXTComm.LCP)) {
                System.err.println("Conexion Fallida");
                jlEstado.setText("Conexion Fallida");
                jlEstado.setToolTipText("Conexion Fallida");
                //System.exit(1);
                PanelConector.conectado = false;
            } else {
                PanelConector.conectado = true;
                NXTCommand.getSingleton().setNXTComm(conn.getNXTComm());
                Motor.A.resetTachoCount();
                Motor.B.resetTachoCount();
                Motor.C.resetTachoCount();
                sensorUltrasonico   = new UltrasonicSensor(SensorPort.S2);
                sensorLuz           = new LightSensor(SensorPort.S1);
                sensorTacto         = new TouchSensor(SensorPort.S3);
                
                Calendar calendario = Calendar.getInstance();
                int hora =calendario.get(Calendar.HOUR_OF_DAY);
                int minutos = calendario.get(Calendar.MINUTE);
                int segundos = calendario.get(Calendar.SECOND);
                
                jlEstado.setText("Conectado: " + hora + ":" + minutos + ":" + segundos);
                jlEstado.setToolTipText("Conectado");
                
                t2 = new Thread(new Runnable() {
                    public void run() {
                        while (true){
                            if (PanelConector.estaConectado()){
                                    keepAlive();
                            }
                            try {
                                    Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                    e.printStackTrace();
                            }
                        }
                    }
                }, "Thread para mantener despierta la conexión");
                t2.start();
            }
        	botonDesconectar.setEnabled(PanelConector.conectado);
            botonConectar.setEnabled(!PanelConector.conectado);
        } catch (Exception e){
    		e.printStackTrace();
    	}
//            jTA_Log.setText("Conectado al NXT "+ Calendar.HOUR_OF_DAY + ":" +
//                        Calendar.MINUTE + ":" + Calendar.SECOND + "\n");
//            jTA_Log.append("----------------\n");

    }//GEN-LAST:event_botonConectarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonConectar;
    private javax.swing.JButton botonDesconectar;
    private javax.swing.JLabel jlEstado;
    // End of variables declaration//GEN-END:variables

    static private boolean conectado = false;
    private NXTConnector conn;

    static private UltrasonicSensor sensorUltrasonico;
    static private LightSensor sensorLuz;
    static private TouchSensor sensorTacto;
    static Thread t2;
}
